package com.tj.jinzao.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import com.tj.jinzao.R;
import com.tj.jinzao.activity.CaseDetailActivity;
import com.tj.jinzao.activity.UploadCaseActivity;
import com.tj.jinzao.adapter.CaseListItemAdapter;
import com.tj.jinzao.base.BaseDataListFragment;
import com.tj.jinzao.callback.IUploadView;
import com.tj.jinzao.manager.UserManager;
import com.tj.jinzao.model.Case;
import com.tj.jinzao.model.Target;
import com.tj.jinzao.presenter.CasePresenter;

import java.util.List;

/**
 * 病例报告列表fragment
 * Created by wjy on 2019/1/1.
 */
public class CaseListFragment extends BaseDataListFragment<List<Case>, CasePresenter, Case> implements IUploadView<List<Case>> {

    public static CaseListFragment newInstance() {
        CaseListFragment fragment = new CaseListFragment();
        if (fragment != null) {
            Bundle args = new Bundle(3);
            args.putString(ARG_TAG_ID, String.valueOf(1));
            args.putInt(ARG_LOAD_ID, 1);
            args.putInt(ARG_LAYOUT_RES_ID, R.layout.base_fragment_item_layout);
            fragment.setArguments(args);
        }
        return fragment;
    }

    @Override
    public void onRequestNoData(String message) {
        setEmptyDataView();
    }

    @Override
    protected CasePresenter createPresenter() {
        return new CasePresenter(this);
    }

    @Override
    protected void createView(LayoutInflater inflater, View view) {
        View headView = inflater.inflate(R.layout.head_case, null);
        headView.setLayoutParams(new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
        mListView.addHeaderView(headView);
        headView.findViewById(R.id.tv_upload).setOnClickListener(this);
        headView.setOnClickListener(this);
        if(getActivity().getIntent().getStringExtra("userId").equals(String.valueOf(UserManager.getInstance().getUser().getId()))){
            headView.setVisibility(View.VISIBLE);
        }else {
            headView.setVisibility(View.GONE);
        }
    }

    @Override
    protected RecyclerView.Adapter createAdapter() {
        return new CaseListItemAdapter(mContext, mData);
    }

    @Override
    protected void requestData() {
        mPresenter.getCaseList(getActivity().getIntent().getStringExtra("userId"));
    }

    @Override
    protected void setEmptyDataView() {
    }

    @Override
    protected boolean resolveData(List<Case> cases) {
        return false;
    }

    @Override
    public void onRequestSuccess(List<Case> list) {
        super.onRequestSuccess(list);
        if (mPageNo <= 1) {
            mData.clear();
        }
        mData.addAll(list);
        showListView();

    }

    @Override
    protected boolean canLoadMoreItems() {
        return false;
    }

    @Override
    public void onResume() {
        super.onResume();
        loadData(true, false);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v.getId() == R.id.tv_upload) {
            startActivity(new Intent(mContext, UploadCaseActivity.class));
        }
    }

    @Override
    protected void initData() {
    }

    @Override
    protected void itemClicked(int position, Case data) {
        startActivity(new Intent(mContext, CaseDetailActivity.class).putExtra("id", data.getId()));
    }

    @Override
    public void onUploadSuccess(String path, String url) {

    }

    @Override
    public void onCaseSuccess(List<Target> list) {

    }

    @Override
    public void onCaseFailed(String msg) {

    }
}
